//
//  FuLisheViewController.m
//  ToListenPhone
//
//  Created by zhibao on 2023/7/18.
//

#import "FuLisheViewController.h"
#import "FLSHaoPingDetailController.h"
#import "FLSFenXiangDetailController.h"
#import "FLSJifenListController.h"
#import "FLSQianDaoController.h"
@interface FuLisheViewController ()

@end

@implementation FuLisheViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.gk_navigationBar.hidden = NO;
    self.gk_backImage = [UIImage imageNamed:@"arrow_back"];
    self.gk_statusBarStyle = UIStatusBarStyleDarkContent;
    [self hideNavLine];
    
    UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];
    [appearance configureWithOpaqueBackground];
    appearance.backgroundColor = UIColor.clearColor;
    //去掉导航栏下的阴影线
    appearance.shadowColor = UIColor.clearColor;
    self.gk_navigationBar.standardAppearance = appearance;
    self.gk_navigationBar.scrollEdgeAppearance = self.gk_navigationBar.standardAppearance;
    
    [Helper setDiferentColors:@[(id)MyColor(@"#F7DFD1").CGColor,(id)MyColor(@"#FFFDFC").CGColor] WithView:self.view withFlag:ColorDirectiondown];
    
    UIButton *oneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    oneBtn.frame = CGRectMake(0, 0, 85, 40);
    [oneBtn setTitle:@"兑换邀请码" forState:UIControlStateNormal];
    [oneBtn setTitleColor:MyColor(@"333333") forState:UIControlStateNormal];
    oneBtn.titleLabel.font = MY_SCALE_SemiboldFONT(14);
    [oneBtn addTarget:self action:@selector(duihuan) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithCustomView:oneBtn];
    self.gk_navRightBarButtonItem = item1;
    
    __weak __typeof(self)weakSelf = self;
    [[HttpHelper sharedInstance] sendGet:@"api/appstore/5star/review/welfare" parameters:@{} success:^(NSURLSessionTask * _Nonnull operation, ResponseModel * _Nonnull responseObject) {
        DLog(@"22222====%@",responseObject.data);
        if([responseObject.data[@"data"][@"type"] intValue] == 0){
            [MYGlobal share].vipDay = [responseObject.data[@"data"][@"value"] intValue];
        }
        if([responseObject.data[@"data"][@"type"] intValue] == 1){
            [MYGlobal share].jifen = [responseObject.data[@"data"][@"value"] intValue];
        }
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        [strongSelf initView];
    } failure:^(NSURLSessionTask * _Nonnull operation, NSError * _Nonnull error) {
        
    }];
}

-(void)initView{
    UIImageView *logoImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"my_fulishe_logo"]];
    [self.view addSubview:logoImgView];
    [logoImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(NAVIH + 20);
        make.right.mas_equalTo(0);
    }];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = @"会员福利社";
    titleLabel.textColor = MYBlock;
    titleLabel.font = MY_SCALE_SemiboldFONT(23);
    [self.view addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(31);
        make.top.equalTo(logoImgView).offset(42);
    }];
    
    UILabel *detailLabel = [UILabel new];
    detailLabel.text = @"豪礼等你拿，会员送不停";
    detailLabel.textColor = MyColor(@"666666");
    detailLabel.font = MY_SCALE_RegularFONT(13);
    [self.view addSubview:detailLabel];
    [detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLabel);
        make.top.equalTo(titleLabel.mas_bottom).offset(10);
    }];
    
    NSArray *content = @[
        @{
            @"title":@"五星好评",
            @"detail":[NSString stringWithFormat:@"五星好评可以获得 %d 天的VIP会员",[MYGlobal share].vipDay],
            @"img":@"my_fulishe_haoping",
        },
        @{
            @"title":@"分享好友",
            @"detail":@"分享好友，获得优惠大礼包",
            @"img":@"my_fulishe_fenxiang",
        },
        @{
            @"title":@"签到",
            @"detail":@"连续签到嬴豪礼，会员等你拿",
            @"img":@"my_fulishe_haoping",
        },
    ];
    
    UIView *tempView;
    for (int i = 0; i < content.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setBackgroundImage:[UIImage imageNamed:content[i][@"img"]] forState:UIControlStateNormal];
        [self.view addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(20);
            if(!tempView){
                make.top.equalTo(logoImgView.mas_bottom).offset(50);
            }else{
                make.top.equalTo(tempView.mas_bottom).offset(10);
            }
            
            make.right.mas_equalTo(-20);
            make.height.mas_equalTo(pixw(125));
        }];
        tempView = btn;
        btn.tag = 1000 + i;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        UILabel *oneLabel = [UILabel new];
        oneLabel.text = content[i][@"title"];
        oneLabel.textColor = MyColor(@"#451705");
        oneLabel.font = MY_SCALE_SemiboldFONT(18);
        [btn addSubview:oneLabel];
        [oneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.mas_equalTo(20);
        }];
        
        UILabel *twoLabel = [UILabel new];
        twoLabel.text = content[i][@"detail"];
        twoLabel.textColor = MyColor(@"#531C07");
        twoLabel.font = MY_SCALE_RegularFONT(12);
        [btn addSubview:twoLabel];
        [twoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(oneLabel);
            make.top.equalTo(oneLabel.mas_bottom).offset(8);
        }];
        
        UILabel *threeLabel = [UILabel new];
        threeLabel.text = @"立即查看 >";
        threeLabel.textColor = MyColor(@"#4B1B06");
        threeLabel.font = MY_SCALE_RegularFONT(12);
        [btn addSubview:threeLabel];
        [threeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(oneLabel);
            make.bottom.equalTo(btn).offset(-20);
        }];
    }
}

-(void)btnClick:(UIButton *)sender{
    if(sender.tag == 1000){
        FLSHaoPingDetailController *controller = [FLSHaoPingDetailController new];
        [self.navigationController pushViewController:controller animated:YES];
    }else if (sender.tag == 1001){
        FLSFenXiangDetailController *controller = [FLSFenXiangDetailController new];
        [self.navigationController pushViewController:controller animated:YES];
    }else{
        FLSQianDaoController *controller = [FLSQianDaoController new];
        [self.navigationController pushViewController:controller animated:YES];
    }
}

-(void)duihuan{

    UIView * bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, MY_Screen_W, MY_Screen_H)];
    bgView.backgroundColor = [MYColor colorWithHexString:@"111111" alpha:0.3];
    [[UIApplication sharedApplication].delegate.window.rootViewController.view addSubview:bgView];
    
    UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(0, MY_Screen_H, 315, 383)];
    contentView.center = CGPointMake(MY_Screen_W / 2, MY_Screen_H / 2);
    contentView.layer.cornerRadius = 15;
    contentView.backgroundColor = UIColor.whiteColor;
    contentView.clipsToBounds = YES;
    [bgView addSubview:contentView];
    
    UIImageView *topImgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, contentView.width, 220)];
    topImgView.image = [UIImage imageNamed:@"fulishe_duihuan_top"];
    [contentView addSubview:topImgView];
    
    UIButton *canCelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    canCelBtn.frame = CGRectMake(contentView.width - 23 - 12, 12, 23, 23);
    [canCelBtn setImage:[UIImage imageNamed:@"fulishe_duihuan_cancel"] forState:UIControlStateNormal];
    [contentView addSubview:canCelBtn];
    [canCelBtn addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
        [bgView removeFromSuperview];
    }]];
    
    UILabel *threeLabel = [UILabel new];
    threeLabel.text = @"兑换邀请码";
    threeLabel.textColor = MyColor(@"#724B22");
    threeLabel.font = MY_SCALE_SemiboldFONT(25);
    threeLabel.textAlignment = NSTextAlignmentCenter;
    [contentView addSubview:threeLabel];
    [threeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(contentView);
        make.top.mas_equalTo(28);
    }];
    
    QMUITextField *codeField = [[QMUITextField alloc]initWithFrame:CGRectMake(23, topImgView.bottom + 25, contentView.width - 46, 46)];
    codeField.backgroundColor = MyColor(@"#EFE8DF");
    codeField.placeholder = @"请输入邀请码";
    codeField.layer.cornerRadius = 23;
    codeField.textAlignment = NSTextAlignmentCenter;
    codeField.placeholderColor = MyColor(@"#BEB3A6");
    codeField.font = MY_SCALE_RegularFONT(18);
    [contentView addSubview:codeField];
    
    UIButton *copyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    copyBtn.frame = CGRectMake(23, codeField.bottom + 25, contentView.width - 46, 46);
    copyBtn.layer.cornerRadius = 23;
    [copyBtn setTitle:@"立即兑换" forState:UIControlStateNormal];
    [copyBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    copyBtn.titleLabel.font = MY_SCALE_SemiboldFONT(18);
    copyBtn.backgroundColor = MyColor(@"#EE6657");
    [contentView addSubview:copyBtn];
    [copyBtn addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        DLog(@"立即兑换");
        if(codeField.text.qmui_trimAllWhiteSpace.length < 3){
            [QMUITips showError:@"请输入邀请码"];
        }
        [[HttpHelper sharedInstance] sendPost:@"api/invitation" parameters:@{@"code":codeField.text.qmui_trimAllWhiteSpace} success:^(NSURLSessionTask * _Nonnull operation, ResponseModel * _Nonnull responseObject) {
            if(responseObject.code == 1){
                [QMUITips showSucceed:@"兑换成功！"];
            }else{
                int err_code = [responseObject.data[@"data"][@"err_code"] intValue];
                if(err_code == 0){
                    [QMUITips showError:@"已兑换过"];
                }else if(err_code == 1){
                    [QMUITips showError:@"邀请码错误"];
                }else if(err_code == 2){
                    [QMUITips showError:@"不能邀请自己"];
                }else{
                    [QMUITips showError:@"未知错误"];
                }
            }
        } failure:^(NSURLSessionTask * _Nonnull operation, NSError * _Nonnull error) {
            
        }];
//        [Helper alertShowWithTitle:@"提示" withContent:LocalizedString(@"恭喜您获得300积分") withSureBtnTitle:LocalizedString(@"去看看") withCancelBtnTitle:LocalizedString(@"我知道了") withSelect:^(NSInteger index) {
//            if(index == 1){
//                [bgView removeFromSuperview];
//                FLSJifenListController *controller = [FLSJifenListController new];
//                [self.navigationController pushViewController:controller animated:YES];
//            }
//        }];
    }]];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
